/*

This program file is part of luacalc
(C) 2006 Richard Simes
You may distribute it under the terms of the GNU General Public License

*/
#include "luacalchelp.h"
#include <iostream>
using namespace std;


HelpFrame::HelpFrame(wxWindow* parent, int id, const wxString& title, const wxPoint& pos, const wxSize& size, long style):
		wxFrame(parent, id, title, pos, size, wxDEFAULT_FRAME_STYLE)
{
	func_label = new wxStaticText(this, -1, wxT("Function List"));
	desc_label = new wxStaticText(this, -1, wxT("Description"));
	desc_panel = new wxPanel(this, -1);

}

HelpFrame::~HelpFrame(){
	delete func_label;
	delete desc_label;
	delete function_list;
	delete desc_panel;
}

void HelpFrame::SetProperties()
{
	SetTitle(wxT("Function Reference"));
	SetSize(wxSize(820, 507));
	Connect(50, 4/*wxEVT_COMMAND_LISTBOX_SELECT*/, wxCommandEventHandler(HelpFrame::OnSelect));
	SetFocus();
}


void HelpFrame::DoLayout()
{
/*
	wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
	sizer->Add(function_list, 0, wxLEFT|wxADJUST_MINSIZE, 0);
	SetAutoLayout(true);
	SetSizer(sizer);
	sizer->Fit(this);
	sizer->SetSizeHints(this);
	Layout();
//*/	
//*
	wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
	sizer->Add(func_label, 0, wxALL|wxEXPAND, 1);
	sizer->Add(function_list, 0, wxEXPAND|wxADJUST_MINSIZE, 0);
	sizer->Add(desc_label, 0, wxALL|wxEXPAND|wxADJUST_MINSIZE, 0);
	sizer->Add(desc_panel, 1, wxEXPAND, 0);
	SetAutoLayout(true);
	SetSizer(sizer);
	sizer->Fit(this);
	sizer->SetSizeHints(this);
	Layout();
//*/
}

void HelpFrame::GetFunctions(lua_State* L)
{

	wxArrayString list; // = new wxArrayString();
	int top;
	lua_getglobal(L, "function_list");
	top = lua_gettop(L);
	lua_pushnil(L);  /* first key */
	while (lua_next(L, top) != 0) {
		/* uses 'key' (at index -2) and 'value' (at index -1) */
		/*cout <<  lua_typename(L, lua_type(L, -2)) << " - " 
		<< lua_typename(L, lua_type(L, -1))  << " : "
		<< lua_tolstring(L, -2, NULL)	<< endl;*/
		list.Add(wxString(lua_tolstring(L, -2, NULL), wxConvUTF8));
		/* removes 'value'; keeps 'key' for next iteration */
		lua_pop(L, 1);
	}
	lua_pop(L, 1);

	function_list = new wxListBox(this, -1, wxDefaultPosition, wxDefaultSize, list, 0);
	SetProperties();
	DoLayout();
	
}

void HelpFrame::OnSelect(wxCommandEvent& event){

	cout << "Hi" << endl;
	exit(9);
	event.Skip();
}
